﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.SqlServerDAL;
using OgilvyOne.Model;
namespace OgilvyOne.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:ActivityTitbitsManageDal
    /// </summary>
    public partial class ActivityTitbitsManageDal : IActivityTitbitsManage
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;
        public ActivityTitbitsManageDal()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name, int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from COFCO_TITBITS");
            strSql.Append(" where TITBITS_NAME=@TITBITS_NAME and TITBITS_ID not in (" + id + ")");
            SqlParameter[] parameters = {
					new SqlParameter("@TITBITS_NAME",name)
            };
            return int.Parse(SQLHelper.ExecuteScalar(connstr, CommandType.Text, strSql.ToString(), parameters).ToString()) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ActivityTitbitsManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"declare @num int;select @num=max(ORDER_NUM)from COFCO_TITBITS;");
            strSql.Append("insert into COFCO_TITBITS(");
            strSql.Append("TITBITS_NAME,TITBITS_HOME_PHOTO,TITBITS_DESC, ORDER_NUM,STATE,MEMBER_ID,ADDTIME,LANGUAGE)");
            strSql.Append(" values (");
            strSql.Append("@TITBITS_NAME,@TITBITS_HOME_PHOTO,@TITBITS_DESC,@num+1,@STATE,@MEMBER_ID,@ADDTIME,@LANGUAGE)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {					
					new SqlParameter("@TITBITS_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@TITBITS_HOME_PHOTO", SqlDbType.VarChar,50),
                    new SqlParameter("@TITBITS_DESC",SqlDbType.VarChar,800),                   
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@MEMBER_ID", SqlDbType.Int,4),
					new SqlParameter("@ADDTIME", SqlDbType.DateTime),
                    new SqlParameter("@LANGUAGE", SqlDbType.VarChar)};
            parameters[0].Value = model.TitbitsName;
            parameters[1].Value = model.TitbitsHomePhoto;
            parameters[2].Value = model.Description;            
            parameters[3].Value = model.State;
            parameters[4].Value = model.MemberId;
            parameters[5].Value = model.AddTime;
            parameters[6].Value = model.Language;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);

        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ActivityTitbitsManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update COFCO_TITBITS set ");
            strSql.Append("TITBITS_NAME=@TITBITS_NAME,");
            strSql.Append("TITBITS_HOME_PHOTO=@TITBITS_HOME_PHOTO,");
            strSql.Append("TITBITS_DESC=@TITBITS_DESC,");
            strSql.Append("ORDER_NUM=@ORDER_NUM,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("MEMBER_ID=@MEMBER_ID,");
            strSql.Append("ADDTIME=@ADDTIME,");
            strSql.Append("LANGUAGE=@LANGUAGE");
            strSql.Append(" where TITBITS_ID=@TITBITS_ID");
            SqlParameter[] parameters = {
					
					new SqlParameter("@TITBITS_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@TITBITS_HOME_PHOTO", SqlDbType.VarChar,50),
                    new SqlParameter("@TITBITS_DESC",SqlDbType.VarChar,800),
                    new SqlParameter("@ORDER_NUM", SqlDbType.Int,4),
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@MEMBER_ID", SqlDbType.Int,4),
					new SqlParameter("@ADDTIME", SqlDbType.DateTime),
                    new SqlParameter("@LANGUAGE", SqlDbType.VarChar),
                    new SqlParameter("@TITBITS_ID", SqlDbType.Int,4)};
            parameters[0].Value = model.TitbitsName;
            parameters[1].Value = model.TitbitsHomePhoto;
            parameters[2].Value = model.Description;
            if (model.OrderNum == null)
            {
                parameters[3].Value = DBNull.Value;
            }
            else
                parameters[3].Value = model.OrderNum;
            parameters[4].Value = model.State;
            parameters[5].Value = model.MemberId;
            if (model.AddTime == null)
            {
                parameters[6].Value = DBNull.Value;
            }
            else
                parameters[6].Value = model.AddTime;
            parameters[7].Value = model.Language;
            parameters[8].Value = model.TitbitsId;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int TITBITS_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_TITBITS ");
            strSql.Append(" where TITBITS_ID=" + TITBITS_ID);
            int rows = SQLHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string TITBITS_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_TITBITS ");
            strSql.Append(" where TITBITS_ID in (" + TITBITS_IDlist + ")  ");

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), null) > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ActivityTitbitsManageModel GetModel(int TITBITS_ID)
        {           
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 TITBITS_ID,TITBITS_NAME,TITBITS_HOME_PHOTO,TITBITS_DESC, ORDER_NUM,STATE,MEMBER_ID,ADDTIME,LANGUAGE from COFCO_TITBITS ");
            strSql.Append(" where TITBITS_ID=" + TITBITS_ID);

            ActivityTitbitsManageModel model = new ActivityTitbitsManageModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["TITBITS_ID"].ToString() != "")
                {
                    model.TitbitsId = int.Parse(ds.Tables[0].Rows[0]["TITBITS_ID"].ToString());
                }
                model.TitbitsName = ds.Tables[0].Rows[0]["TITBITS_NAME"].ToString();
                model.TitbitsHomePhoto = ds.Tables[0].Rows[0]["TITBITS_HOME_PHOTO"].ToString();
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MEMBER_ID"].ToString() != "")
                {
                    model.MemberId = int.Parse(ds.Tables[0].Rows[0]["MEMBER_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ADDTIME"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["ADDTIME"].ToString());
                }

                if (ds.Tables[0].Rows[0]["ORDER_NUM"].ToString() != "")
                {
                    model.OrderNum = int.Parse(ds.Tables[0].Rows[0]["ORDER_NUM"].ToString());
                }
                model.Description = ds.Tables[0].Rows[0]["TITBITS_DESC"].ToString();
                if (ds.Tables[0].Rows[0]["LANGUAGE"].ToString() != "")
                {
                    model.Language = ds.Tables[0].Rows[0]["LANGUAGE"].ToString();
                }               
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ActivityTitbitsManageModel GetModelByOrderNum(int orderNum)
        {            
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 TITBITS_ID,TITBITS_NAME,TITBITS_HOME_PHOTO,TITBITS_DESC, ORDER_NUM,STATE,MEMBER_ID,ADDTIME from COFCO_TITBITS ");
            strSql.Append(" where ORDER_NUM=" + orderNum);

            ActivityTitbitsManageModel model = new ActivityTitbitsManageModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["TITBITS_ID"].ToString() != "")
                {
                    model.TitbitsId = int.Parse(ds.Tables[0].Rows[0]["TITBITS_ID"].ToString());
                }
                model.TitbitsName = ds.Tables[0].Rows[0]["TITBITS_NAME"].ToString();
                model.TitbitsHomePhoto = ds.Tables[0].Rows[0]["TITBITS_HOME_PHOTO"].ToString();
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MEMBER_ID"].ToString() != "")
                {
                    model.MemberId = int.Parse(ds.Tables[0].Rows[0]["MEMBER_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ADDTIME"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["ADDTIME"].ToString());
                }

                if (ds.Tables[0].Rows[0]["ORDER_NUM"].ToString() != "")
                {
                    model.OrderNum = int.Parse(ds.Tables[0].Rows[0]["ORDER_NUM"].ToString());
                }
                model.Description = ds.Tables[0].Rows[0]["TITBITS_DESC"].ToString();
                if (ds.Tables[0].Rows[0]["LANGUAGE"].ToString() != "")
                {
                    model.Language = ds.Tables[0].Rows[0]["LANGUAGE"].ToString();
                }   
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TITBITS_ID,TITBITS_NAME,TITBITS_HOME_PHOTO,TITBITS_DESC, ORDER_NUM,STATE,MEMBER_ID,ADDTIME,LANGUAGE FROM COFCO_TITBITS");           
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by ORDER_NUM");
            return SQLHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" TITBITS_ID,TITBITS_NAME,TITBITS_HOME_PHOTO,TITBITS_DESC, ORDER_NUM,STATE,MEMBER_ID,ADDTIME,LANGUAGE  ");
            strSql.Append(" FROM COFCO_TITBITS ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (!String.IsNullOrEmpty(filedOrder.Trim()))
            {
                strSql.Append(" order by " + filedOrder);
            }
            return SQLHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "COFCO_TITBITS";
            parameters[1].Value = "TITBITS_ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return SQLHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/
        public int getNum(string str)
        {
            string strSql = "select count(*)from [COFCO_TITBITS] where 1=1 and "+str;
            return int.Parse(SQLHelper.ExecuteScalar(connstr, CommandType.Text, strSql.ToString(), null).ToString());
        }
        #endregion  Method
    }
}

